//
//  CampusListSearchViewController.m
//  ShowYork
//
//  Created by hxh on 2021/3/26.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusListSearchViewController.h"
#import "CampusDetailViewController.h"
#import "CampusListSearchView.h"
#import "CampusListSearchItemCell.h"
#import "CampusSchoolItemModel.h"
@interface CampusListSearchViewController ()<CampusListSearchViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)CampusListSearchView *searchView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UILabel *noDataLb;
@property (nonatomic,copy)NSString *searchKey;
@property (nonatomic,strong)NSURLSessionTask *task;
@end

@implementation CampusListSearchViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = YES;
    [IQKeyboardManager sharedManager].enable = NO;
    self.navigationController.navigationBar.translucent = NO;
    
  
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = NO;
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    [self registNote];
    
    
}
- (void)setUI{
    
    self.isHideNavBar = YES;
    
    self.searchView = [[CampusListSearchView alloc]init];
    self.searchView.delegate = self;
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(GK_STATUSBAR_HEIGHT);
        make.height.mas_equalTo(44);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.noDataLb = [[UILabel alloc]init];
    self.noDataLb.text = @"无内容";
    self.noDataLb.textColor = C153;
    self.noDataLb.font = font(12);
    self.noDataLb.hidden = YES;
    
    [self.view addSubview:self.noDataLb];
    
    [self.noDataLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-100);
    }];
}

- (void)registNote{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma maark - 监听键盘时间
- (void)keyboardWillShow:(NSNotification *)notification
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGRect keyboardBounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, keyboardBounds.size.height, 0);
    });
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    self.tableView.contentInset = UIEdgeInsetsZero;
}


#pragma mark - CampusListSearchViewDelegate
- (void)cancel{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)textChange:(NSString *)text{
    self.noDataLb.hidden = YES;
    if ([text cl_removeSpaceString].length != 0) {
        self.searchKey = text;
        [self loadDataSource];
    }else{
        self.searchKey = @"";
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
    }
}
- (void)textSearch:(NSString *)text{
   
    [self.view endEditing:YES];
    
    [self textChange:text];
}
-(void)textSpace{
    
    self.searchKey = @"";
    self.noDataLb.hidden = YES;
    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
}
#pragma mark - network
- (void)loadDataSource{
    
    [self.task cancel];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:GetSchoolByName];
    param[@"name"] = self.searchKey;
    self.task = [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(GetSchoolByName) parames:param success:^(id responseObj) {
        self.dataArray =  [CampusSchoolItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE]];
        self.noDataLb.hidden = self.dataArray.count > 0;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [WHToast showErrorWithMessage:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CampusListSearchItemCell *cell = [CampusListSearchItemCell cellWithTableView:tableView];
    [cell setItemModel:self.dataArray[indexPath.item]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 50.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [self.view endEditing:YES];
    
    [MobClick event:@"show_school_detail"];
    
    CampusSchoolItemModel *itemModel = self.dataArray[indexPath.item];
    
    CampusDetailViewController *vc = [[CampusDetailViewController alloc]init];
    vc.schId = itemModel.id;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - other

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
